--[[ PROTOTYPE
state= {
	name = 'name',
	starting = {
		functions,
		set currenStatus to runing		
	},
	
	runing = {-
		functions,
		conditions for leving the state
	} 
	
	ending = {
		functions,
		decision which stat will be set 
	}
}

t will allways be holding the path to the calling instance 

all this states can be hold by in child classes of gameelement 
---]]

States = {
	
default	= { 
	name = "default",
	
	running = {


			function(t) 
				if math.random(1,30) == 1 then t.dir = directions[math.random(1,8)] 
				end 
			end,	


			function (t)
				t:walk()
			end

		
	}		

},
	
control = {
	name = 'control',
	
 	starting = {
		function(t)
			t.currentState = States.control.running
		end
		
			
	},
	running = {
		-- -- -- -- -- -- -- --
		-- -- STATUSCHECK -- --   TODO: remove this to the right place
		-- -- -- -- -- -- -- --
		--[[function (t)--hero.statuscheck() -- basic, experience, health, mana, oht status check. nearly in every state avaible
	 		if t.hp <= 0 then t.currentState = t.states['dead'] end -- TODO: changestate function
			if t.oht.status == 1 then t.oht.run() end
		 	if t.mana < t.manaMax then t:regenerateHp()end
		 	if t.hp < t.hpMax then t:regenerateHp()end
		 	if t.exp > t.level * 20 then t:levelUp() end
		end,--]]

		-- -- -- -- -- -- -- --
		-- -- CONTROLS -- -- -- 
		-- -- -- -- -- -- -- --
		function(t)
 			if controls.button1 == 'pressed' then -- TODO: controller basic to do ... !!
			end
		end,
	
		function (t)
			if controls.button5 == 'pressed'
			then game.status = 1 
			end
		end,
	
		function (t)
			if controls.button6 == 'pressed'
			then game.status = 2 
			end
		end,

		function (t) --hero.move.up()
		  if controls.up == 'held' then
			t.dir = 'north'
		  end
		end,

		function (t)
		  if controls.right == 'held' and controls.up == 'held' then
			t.dir = 'northeast'
		  end
		end,

		function (t)
		  if controls.right == 'held' then
			t.dir = 'east'
		  end
		end,

		function (t)
		  if controls.right == 'held' and controls.down == 'held'  then
			t.dir = 'southeast'
		  end
		end,

		function (t)
		  if controls.down == 'held' then
			t.dir = 'south'
		  end
		end,

		function (t)
		  if controls.down == 'held' and controls.left == 'held'  then
			t.dir = 'southwest'
		  end
		end,

		function (t)
		  if controls.left == 'held' then
			t.dir = 'west'
		  end
		end,	

		function (t)
		  if controls.left == 'held' and controls.up == 'held' then
			t.dir = 'northwest'
		  end
		end,
		
		function (t)
			t:walk()
		end
		
		},
		  ending = {},
		  process = 'running'
},
		
dead = {
	name = 'dead',
	starting = {},
	running = {
		function ()
			screen.print(SCREEN_DOWN, 50,50, "Du bis gestorben") -- Falls held tot ist erscheint diese Botschaft
		end
	},
	ending = {
		function ( t )
			if t.hp then
				t.currentState = States.idle.running
			end
		end
	},
	process = 'running'
},
---------------------
--States for enemies
--------------------
idle = {
	name = 'idle',
	starting = {
		function ( t )
			t.currentState = States.idle.running
		end
	},
	running = {
		function(t) 
			if math.random(1,30) == 1 then t.dir = directions[math.random(1,8)] 
			end 
		end,
		-- search function for enemy's idle state
		function (t) -- checks if the monster is to hunt the hero
			if compare( (t.x - t.searchRange), (t.y - t.searchRange), (t.searchRange * 2 + t.wid) , (t.searchRange * 2 + t.hig), hero.x,hero.y, hero.wid, hero.hig) == true
			then 	
		    	t.currentState = States.idle.ending
		    -- TODO: instead of line above use a changestate function, which clears tmp data and sets process of old state to 'starting'
		    	runstate(t)
			end	
		end
	},
	ending = {
		function ( t )
			t.currentState = States.hunt.starting
		end
		}
	
},
	
hunt = { 
  	name = 'hunt',
	starting = {
		function ( t )
			t.currentState = States.hunt.running
		end
	},
	running = {
		function(t)
			if compare( (t.x - t.searchRange), (t.y - t.searchRange), (t.wid  + t.searchRange) , (t.searchRange + t.hig), hero.x, hero.y, hero.wid, hero.hig) == true then 
	  			t.currentState = States.attack.starting
	  		else
				if	t.x == hero.x and
					t.y > hero.y then t.dir = 'north'
				elseif 	t.x < hero.x and
					t.y == hero.y then t.dir = 'northeast'
				elseif 	t.x == hero.x and
					t.y < hero.y then t.dir = 'east'
				elseif 	t.x > hero.x and
					t.y == hero.y then t.dir = 'southeast'
				elseif 	t.x < hero.x and
					t.y > hero.y then t.dir = 'south'
				elseif 	t.x < hero.x and
					t.y < hero.y then t.dir = 'southwest'
				elseif 	t.x > hero.x and
					t.y < hero.y then t.dir = 'west'
				elseif 	t.x > hero.x and
					t.y > hero.y then t.dir = 'northwest'
				end		
			end
		end
	},
	ending = {
	}
},

attack = { 
	name = 'attack',
	starting = {
		function ( t )
			t.currentState = States.attack.running	
		end
	},
	running = {
	    function(t) 
	    	-- TODO: check if in range. if not change state to hunt
	      -- if self.attacklimit < clock.second then
	    		-- self.attacklimit = clock.second + 1 * self.attackrate
	    		--if self.attackrate > 59 then self.attackrate = self.attackrate - 60 end
	    		hero.hp = hero.hp - 2 
	    
		end 
	-- end
	  },
	ending = {
	},
	process = 'running'
}

}